<?php
// No directAcces
defined( '_AppBackMiage' ) or die( 'Restricted access' );
if(isAdministrateur ()){
    include_once (DS."backoffice/include/header.php");
    include_once( DS."backoffice/include/toppanel_administration.php");

    $title = "Liste des documents obligatoires";
    $add = '<a href="' . URL . 'index.php?m=admin_administration_doc_obligatoires_add" class="add">Ajouter un document</a>';
    
    if(isset($_GET['triParPromo']) && !empty($_GET['triParPromo'])){
        $byPromo = $_GET['triParPromo'];
    }
    else {
        $byPromo = "";
    }

    $listeDoc = TypeDocument::getListTypeDocument($byPromo);
    $lstPromo = Promo::getListPromo();
    ?>

    <div id="wrapper">
        <div id="content">
            <div id="rightnow">

                <h3 class="reallynow">
                    <span><?php echo $title; ?></span>
                    <?php echo $add;?>
                    <br />
                </h3>

                <?php
 // SUPPRESSION
                if (isset($_GET['delete']) && !empty($_GET['delete'])) {
                    $doc = new TypeDocument($_GET['delete']);
                    $yes = '<a href="' . URL . 'index.php?m=admin_administration_doc_obligatoires&delete='.$doc->getIdType().'&confirm=ok">Oui</a>';
                    $no =  '<a href="' . URL . 'index.php?m=admin_administration_doc_obligatoires">Non</a>';
                    if (isset($_GET['confirm']) && !empty($_GET['confirm'])) {
                        if ($_GET['confirm'] === "ko") {
                            ?>
                            <div align="center">
                                <p class="youhave" >
                                    &Ecirc;tes vous certain de vouloir supprimer "<?php echo $doc->getNomType(); ?>" ?
                                    <br/>

                                        <div class="lien" >
                                            <?php echo $yes; ?>
                                        </div>

                                        <div class="lien">
                                            <?php echo $no; ?>
                                        </div>

                                </p>
                            </div>
                            <?php
                        }
                        else {
                            $doc->delete();
                            $listeDoc = TypeDocument::getListTypeDocument($byPromo);
                            //Comme on vient de supprimer un doc on réactualise la lise
                            ?>
                            <form action="<?php echo URL; ?>index.php" method="get">
                                <input type="hidden" name="m" value="admin_administration_doc_obligatoires"/>
                                <label for="triParPromo">Promo : </label>
                                <select name="triParPromo">
                                    <option value="">-- Tous --</option>
                                    <?php
                                    foreach ($lstPromo as $k => $e) {
                                        ?>
                                        <option value="<?php echo $k; ?>" <?php if ($byPromo == $k)
                                                echo "selected='selected' "; ?>><?php echo $e; ?></option>
                                        <?php
                                    }
                                    ?>
                                </select>
                                <input type="submit" value="Rechercher"/>
                            </form>

                            <table>
                                <thead>
                                    <tr>
                                        <th>
                                            Nom du document
                                        </th>
                                        <th>
                                            Date limite de rendu
                                        </th>
                                        <th>
                                            Promo
                                        </th>
                                        <th>
                                            Action
                                        </th>
                                    </tr>
                                </thead>


                                <tbody>
                                                <?php
                                foreach($listeDoc as $document){
                                    $deleteURL = URL."/index.php?m=admin_administration_doc_obligatoires&delete=".$document->IdTypeDocument."&confirm=ko";
                                    $editURL = URL."/index.php?m=admin_administration_doc_obligatoires_edit&id=".$document->IdTypeDocument;
                                    $promo = new Promo($document->IdPromo);
                                    ?>
                                    <tr>
                                        <td>
                                            <label for="administration"><?php echo stripslashes($document->NomTypeDocument);?></label>
                                        </td>
                                        <td class="a-center">
                                            <?php echo date('j/n/y', $document->DateDeadlineRendu ); ?>
                                        </td>
                                        <td class="a-center">
                                            <?php echo $promo->getNom(); ?>
                                        </td>
                                        <td class="a-center">
                                            <a href="<?php echo $editURL ?>">
                                                <img src="<?php echo STYLEBK; ?>img/icons/page_white_edit.png" title="Editer le document" width="16" height="16" />
                                            </a>
                                            <a href="<?php echo $deleteURL ?>">
                                                <img src="<?php echo STYLEBK; ?>img/icons/cross.png" title="Supprimer document" width="16" height="16" />
                                            </a>
                                        </td>
                                    </tr>
                                <?php
                                }
                                ?>
                                </tbody>
                            </table>
                            <?php
                            
                        }
                    }
                }
//PAGE NORMALE
                else {
                    /*** Zone de recherche ***/
                    ?>
                    <form action="<?php echo URL; ?>index.php" method="get">
                        <input type="hidden" name="m" value="admin_administration_doc_obligatoires"/>
                        <label for="triParPromo">Promo : </label>
                        <select name="triParPromo">
                            <option value="">-- Tous --</option>
                            <?php
                            foreach ($lstPromo as $k => $e) {
                                ?>
                                <option value="<?php echo $k; ?>" <?php if ($byPromo == $k)
                                        echo "selected='selected' "; ?>><?php echo $e; ?></option>
                                <?php
                            }
                            ?>
                        </select>
                        <input type="submit" value="Rechercher"/>
                    </form>
                    <!--*** Fin de zone de recherche ***-->
                    <table>
                        <thead>
                            <tr>
                                <th>
                                    Nom du document
                                </th>
                                <th>
                                    Date limite de rendu
                                </th>
                                <th>
                                    Promo
                                </th>
                                <th>
                                    Action
                                </th>
                            </tr>
                        </thead>


                        <tbody>
                                        <?php
                        foreach($listeDoc as $document){
                            $deleteURL = URL."/index.php?m=admin_administration_doc_obligatoires&delete=".$document->IdTypeDocument."&confirm=ko";
                            $editURL = URL."/index.php?m=admin_administration_doc_obligatoires_edit&id=".$document->IdTypeDocument;
                            $promo = new Promo($document->IdPromo);
                            ?>
                            <tr>
                                <td>
                                    <label for="administration"><?php echo stripslashes($document->NomTypeDocument);?></label>
                                </td>
                                <td class="a-center">
                                    <?php echo date('j/n/y', $document->DateDeadlineRendu ); ?>
                                </td>
                                <td class="a-center">
                                    <?php echo $promo->getNom(); ?>
                                </td>
                                <td class="a-center">
                                    <a href="<?php echo $editURL ?>">
                                        <img src="<?php echo STYLEBK; ?>img/icons/page_white_edit.png" title="Editer le document" width="16" height="16" />
                                    </a>
                                    <a href="<?php echo $deleteURL ?>">
                                        <img src="<?php echo STYLEBK; ?>img/icons/cross.png" title="Supprimer document" width="16" height="16" />
                                    </a>
                                </td>
                            </tr>
                        <?php
                        }
                        ?>
                        </tbody>
                    </table>
                <?php
                }
                ?>
            </div>
        </div>

    <?php
    include_once (DS."backoffice/include/sidebar.php");
    include_once (DS."backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>